package com.iyjhsoc.game.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iyjhsoc.game.dao.*;
import com.iyjhsoc.game.domain.VO.GameKillInfoVO;
import com.iyjhsoc.game.domain.VO.PlayerChooseVO;
import com.iyjhsoc.game.domain.dto.*;
import com.iyjhsoc.game.domain.entity.CompetitionDO;
import com.iyjhsoc.game.domain.entity.GameRoleDO;
import com.iyjhsoc.game.domain.entity.PlayerChooseDO;
import com.iyjhsoc.game.domain.response.Response;
import com.iyjhsoc.game.service.CompetitionSmallGameService;
import com.iyjhsoc.game.service.PlayerChooseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class PlayerChooseServiceImpl extends ServiceImpl<PlayerChooseDAO,PlayerChooseDO> implements PlayerChooseService {
    @Resource
    PlayerChooseDAO playerChooseDAO;
    @Resource
    CompetitionSmallGameDAO competitionSmallGameDAO;
    @Resource
    CompetitionDAO competitionDAO;
    @Resource
    GameKillInfoDAO gameKillInfoDAO;
    @Resource
    GameRoleDAO game;

    @Override
    public List<CompetitionSmallTeamInfoDTO> getGameChoose(PlayerChooseVO vo) {
        CompetitionDO competitionDO = competitionDAO.selectById(vo.getCompetitionId());
        List<CompetitionSmallGameDTO> gameIds = competitionSmallGameDAO.queryTeamGameHistory(competitionDO.getTeamOneId(), competitionDO.getTeamTowId());
        List<CompetitionSmallTeamInfoDTO> res = new ArrayList<>(gameIds.size());
        for(CompetitionSmallGameDTO competitionSmallGameDTO: gameIds){
            vo.setCompetitionSmallGameId(competitionSmallGameDTO.getId().toString());
            List<PlayerChooseDTO> gameChoose = playerChooseDAO.getGameChoose(vo);
            Map<String,List<PlayerChooseDTO>> teamList = new HashMap<>();
            for(PlayerChooseDTO playerChooseDTO:gameChoose){
                List<PlayerChooseDTO> playerChooseDTOS;
                if(teamList.containsKey(playerChooseDTO.getTeamId())){
                    playerChooseDTOS = teamList.get(playerChooseDTO.getTeamId());
                } else {
                    playerChooseDTOS = new ArrayList<>();
                    teamList.put(playerChooseDTO.getTeamId(),playerChooseDTOS);
                }
                playerChooseDTOS.add(playerChooseDTO);
            }
            if(teamList == null || teamList.size() == 0){
                continue;
            }
            CompetitionSmallTeamInfoDTO competitionSmallTeamInfoDTO = new CompetitionSmallTeamInfoDTO();
            competitionSmallTeamInfoDTO.setPositionMapA(teamList.get(competitionDO.getTeamOneId().toString()));
            competitionSmallTeamInfoDTO.setPositionMapB(teamList.get(competitionDO.getTeamTowId().toString()));
            competitionSmallTeamInfoDTO.setGameName(competitionSmallGameDTO.getTeamOneName() + " VS " +competitionSmallGameDTO.getTeamTowName());
            competitionSmallTeamInfoDTO.setGameTime(competitionSmallGameDTO.getMatchTime());
            competitionSmallTeamInfoDTO.setVictoryTeam(competitionSmallGameDTO.getVictoryTeamId().equals(competitionDO.getTeamOneId())?"1":"2");
            competitionSmallTeamInfoDTO.setCompetitionDate(competitionSmallGameDTO.getCompetitionDate());
            GameKillInfoVO gameKillInfoVO = new GameKillInfoVO();
            gameKillInfoVO.setCompetitionSmallGameId(Long.valueOf(competitionSmallGameDTO.getId()));
            List<GameKillInfoDTO> gameKillInfoDTOS = gameKillInfoDAO.selectGameOneKill(gameKillInfoVO);
            if(gameKillInfoDTOS != null && gameKillInfoDTOS.size()>0) {
                competitionSmallTeamInfoDTO.setOneKill(gameKillInfoDTOS.get(0));
            }
            competitionSmallTeamInfoDTO.setMatchId(competitionSmallGameDTO.getMatchId());
            competitionSmallTeamInfoDTO.setCompetitionSmallGameId(competitionSmallGameDTO.getId());
            res.add(competitionSmallTeamInfoDTO);
        }
        return res;
    }

    @Override
    public Response getSmallGamePlayerInfoById(String smallGameId) {
        PlayerChooseVO vo = new PlayerChooseVO();
        vo.setCompetitionSmallGameId(smallGameId);
        List<PlayerChooseDTO> gameChoose = playerChooseDAO.getGameChoose(vo);
        Map<String,Map<String,PlayerChooseDTO>> res = new HashMap<>();
        for(PlayerChooseDTO playerChooseDTO:gameChoose){
            Map<String,PlayerChooseDTO> team;
            if(res.containsKey(playerChooseDTO.getPosition())){
                team = res.get(playerChooseDTO.getPosition());
            } else {
                team = new HashMap<>();
                res.put(playerChooseDTO.getPosition(),team);
            }
            team.put(playerChooseDTO.getTeamId(),playerChooseDTO);
        }
        return Response.SUCCESS(res);
    }

    @Override
    public Response getMatchPlayerData(PlayerChooseVO vo) {
        List<PlayerChooseDTO> playerChooseDTOS = playerChooseDAO.gameMatchChoose(vo);
        Map<String,PlayerRoleDTO> map = new HashMap<>();
        int victory = 0;
        for(PlayerChooseDTO playerChooseDTO: playerChooseDTOS){
            PlayerRoleDTO playerRoleDTO;
            if(map.containsKey(playerChooseDTO.getGameRoleId())){
                playerRoleDTO = map.get(playerChooseDTO.getGameRoleId());
                playerRoleDTO.setNum(playerRoleDTO.getNum()+1);
                if("1".equals(playerChooseDTO.getIsMvp())) {
                    playerRoleDTO.setMvpNum(playerRoleDTO.getMvpNum()+1);
                }
                playerRoleDTO.setRecord(playerRoleDTO.getRecord() +","+playerChooseDTO.getKillNum()+"/"+ playerChooseDTO.getDeathNum() + "/" + playerChooseDTO.getAssistsNum()+",");
                if(playerChooseDTO.getTeamId().equals(playerChooseDTO.getVictoryTeamId())){
                    playerRoleDTO.setVictory(playerRoleDTO.getVictory()+1);
                }

            } else  {
                playerRoleDTO = new PlayerRoleDTO();
                map.put(playerChooseDTO.getGameRoleId(),playerRoleDTO);
                playerRoleDTO.setNum(1);
                GameRoleDO gameRoleDO = game.selectById(playerChooseDTO.getGameRoleId());
                playerRoleDTO.setName(gameRoleDO.getName());
                playerRoleDTO.setPlayerId(playerChooseDTO.getPlayerId());
                if("1".equals(playerChooseDTO.getIsMvp())) {
                    playerRoleDTO.setMvpNum(1);
                } else {
                    playerRoleDTO.setMvpNum(0);
                }
                if(playerChooseDTO.getTeamId().equals(playerChooseDTO.getVictoryTeamId())){
                    playerRoleDTO.setVictory(1);
                } else {
                    playerRoleDTO.setVictory(0);
                }
                playerRoleDTO.setRecord(playerChooseDTO.getKillNum()+"/"+ playerChooseDTO.getDeathNum() + "/" + playerChooseDTO.getAssistsNum());
            }
        }

        return Response.SUCCESS(map.values());
    }
}
